cmake_minimum_required(VERSION 3.21)
# Target definition
set(CMAKE_SYSTEM_NAME  Generic)
set(CMAKE_SYSTEM_PROCESSOR cortex-m3)

# Perform compiler test with static library
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

#---------------------------------------------------------------------------------------
# Set compilers
#---------------------------------------------------------------------------------------
set(CMAKE_C_COMPILER armclang CACHE INTERNAL "C Compiler")
set(CMAKE_CXX_COMPILER armclang CACHE INTERNAL "C++ Compiler")
set(CMAKE_ASM_COMPILER armasm CACHE INTERNAL "ASM Compiler")
set(CMAKE_LINKER armlink)
set(CMAKE_OBJCOPY fromelf)

# Set suffix of binary
set(CMAKE_EXECUTABLE_SUFFIX_C        .axf)
set(CMAKE_EXECUTABLE_SUFFIX_CXX      .axf)
set(CMAKE_C_OUTPUT_EXTENSION         .o)
set(CMAKE_CXX_OUTPUT_EXTENSION       .o)
set(CMAKE_STATIC_LIBRARY_SUFFIX_C    .a)
set(CMAKE_STATIC_LIBRARY_SUFFIX_CXX  .a)

# Project config
project(test C ASM)

set(CMAKE_C_FLAGS "-xc -std=c99 --target=arm-arm-none-eabi -mcpu=cortex-m3 -c -fno-rtti -fsigned-char -fshort-enums -fshort-wchar -D__MICROLIB -gdwarf-4 -O0 -ffunction-sections -Wno-packed -Wno-missing-variable-declarations -Wno-missing-prototypes -Wno-missing-noreturn -Wno-sign-conversion -Wno-nonportable-include-path -Wno-reserved-id-macro -Wno-unused-macros -Wno-documentation-unknown-command -Wno-documentation -Wno-license-management -Wno-parentheses-equality -MD")
set(CMAKE_ASM_FLAGS "--cpu Cortex-M3 -g --pd \"__MICROLIB SETA 1\"")

set(CMAKE_VERBOSE_MAKEFILE ON)

# Header directories
include_directories("../include/cmsis")
include_directories("../include/drivers")
include_directories("../include/mm32f3277")
include_directories("../include/zf_common")
include_directories("../include/zf_driver")
include_directories("../include/zf_device")
include_directories("../include/fatfs")
include_directories("user/inc")

# Source directories
set(SRC_DIR ${PROJECT_SOURCE_DIR}/)

aux_source_directory(${SRC_DIR}/user/src PRJ_SRCS)

##set(CMAKE_FIND_LIBRARY_PREFIXES "")
##set(CMAKE_FIND_LIBRARY_SUFFIXES ".lib")

find_library(Seekfree_Lib seekfree "../lib")

add_link_options(--cpu Cortex-M3 --library_type=microlib)
##add_link_options(--ro-base 0x08000000  --rw-base 0x20000000 --entry Reset_Handler --first __Vectors)
add_link_options(--scatter ../config/mm32f327x_g9p.sct)
add_link_options(--strict --summary_stderr --map --xref --callgraph --symbols  --info=stack,sizes,summarysizes,totals,unused,veneers)


set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "bin")
add_executable(${PROJECT_NAME} ${PRJ_SRCS})
target_link_libraries(${PROJECT_NAME} ${Seekfree_Lib})

# Optional: Create hex, bin and S-Record files after the build
set(AXF_FILE_NAME ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${PROJECT_NAME}.axf)
set(HEX_FILE_NAME ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${PROJECT_NAME}.hex)
add_custom_command(TARGET ${PROJECT_NAME}
    POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} --i32combined --output ${HEX_FILE_NAME} ${AXF_FILE_NAME}
)